//
//  WXCloudNewsListVideoViewModel.m
//  xinguiyang
//
//  Created by 段尹斌 on 2017/12/7.
//  Copyright © 2017年 voc. All rights reserved.
//

#import "WXCloudNewsListVideoViewModel.h"
#import "WXCloudVideoNewsAPIManager.h"

#import "NSArray+Array.h"
#import "NSArray+Extension.h"
#import "NSDictionary+Extension.h"
#import "WXMediator_EXTERN.h"
#import <WXCloudModuleMediator/WXMediator+WXMediatorModuleModelManager.h>
#import <WXCloudModuleCacheManager/WXCacheManager.h>
#import <WXCloudPluginProgressHUD/WXProgressHUD.h>
#import "WXCloudModuleNotificationKey.h"

@interface WXCloudNewsListVideoViewModel ()<WXAPIManagerApiCallBackDelegate,WXAPIManagerParamSource>


/** 新闻列表API */
@property (nonatomic, strong) WXCloudVideoNewsAPIManager *cloudVideoNewsAPIManager;

/** 轮播图模型列表 */
@property (nonatomic, copy, readwrite) NSArray *cycleViewModelList;
/** 功能入口模型列表 */
@property (nonatomic, copy, readwrite) NSArray *functionModelList;
/** 新闻列表模型列表 */
@property (nonatomic, copy, readwrite) NSArray *homeNewsModelList;
/** 是否加载下一页 */
@property (nonatomic, assign) BOOL isNextLoad;
@property (nonatomic, assign) BOOL isHadLoadAD;

/** 轮播图通用功能入口组装ID（用于缓存数据） */
@property (nonatomic, copy, readonly) NSString *functionID;

@end

@implementation WXCloudNewsListVideoViewModel
- (instancetype)init
{
    if (self = [super init]) {
        [self initialize];
    }
    return self;
}
/** 配置内部参数 */
- (void)initialize
{
    
    self.isHadLoadAD = NO;
}

#pragma mark -WXNewsTableViewModelDelegate
- (void)tableViewHeaderRereshing
{
    [self requestHomeNewsWithRequestType:WXNewsRequestTypeFirst];
}
- (void)tableViewFooterRereshing
{
    [self requestHomeNewsWithRequestType:WXNewsRequestTypeNext];
}

#pragma mark -自定义代理
#pragma mark -WXAPIManagerParamSource
- (NSDictionary *)paramsForApi:(WXAPIBaseManager *)manager
{
    NSDictionary *params = @{};
    if (manager == self.cloudVideoNewsAPIManager) {
        params = @{
                   @"action":@"get_news_list",
                   @"classid":_classID
                   };
    }
    return params;
}
#pragma mark -WXAPIManagerApiCallBackDelegate
/** 成功的回调 */
- (void)managerCallAPIDidSuccess:(WXAPIBaseManager *)manager
{
    NSDictionary *responseData = [manager fetchDataWithReformer:nil];
    /** 轮播图 */
   if (manager == self.cloudVideoNewsAPIManager) {
        NSDictionary *data = responseData[@"data"];
        
        if (![data isKindOfClass:[NSDictionary class]]) {
            self.isRequestNewsDataSuccess = NO;
            return;
        }
        _cloudVideoNewsAPIManager.total = [data[@"total"] integerValue];
        if (!_cloudVideoNewsAPIManager.isNextLoad) {
            if (_cloudVideoNewsAPIManager.total<=0) {
                self.homeNewsModelList = nil;
                self.functionModelList=nil;
            } else {
                NSArray *functionList=responseData[@"data"][@"menu"];
                functionList = [functionList modifyObjectValue:@"7" forKey:kWXMediatorTableModelCustomCellTypeKey];
            self.functionModelList=             [[WXMediator sharedInstance] translateServiceDataToAdapterModelList:functionList];
                NSArray *tempList = [[WXMediator sharedInstance] translateServiceDataToAdapterModelList:data[@"data"]];
                NSMutableArray *tempArrM = [NSMutableArray arrayWithArray:tempList];
                self.homeNewsModelList = [self handleADDataWithNewslIst: [tempArrM copy]];
                //只缓存第一页的数据
                [[WXCacheManager sharedInstance] insertOrUpdateCacheModelWithChanelID:self.classID tableNameType:WXCacheTableTypeNewsModule dataList:self.homeNewsModelList];
                
                [[WXCacheManager sharedInstance] insertOrUpdateCacheModelWithChanelID:self.functionID tableNameType:WXCacheTableTypeNewsModule dataList:self.functionModelList];
            }
        } else {
            NSArray *tempList =  [[WXMediator sharedInstance] translateServiceDataToAdapterModelList:data[@"data"]];
            tempList = [NSArray arrayWithTempList:tempList array:_homeNewsModelList];
            self.homeNewsModelList =[self handleADDataWithNewslIst:tempList];
        }
        self.isRequestNewsDataSuccess = YES;
    }
}
/** 失败回调 */
- (void)managerCallAPIDidFailed:(WXAPIBaseManager *)manager
{
    if (manager == self.cloudVideoNewsAPIManager){
        /** 请求缓存数据 */
        NSArray *tempDataList = [[WXCacheManager sharedInstance] findCacheModelWithChanelID:_classID tableNameType:WXCacheTableTypeNewsModule];
        self.homeNewsModelList = [[WXMediator sharedInstance] translateCollectionDataToAdapterModelList:tempDataList];
        NSArray *tempfunList = [[WXCacheManager sharedInstance] findCacheModelWithChanelID:self.functionID tableNameType:WXCacheTableTypeNewsModule];
        self.functionModelList = [[WXMediator sharedInstance] translateCollectionDataToAdapterModelList:tempfunList];        self.isRequestNewsDataSuccess = NO;
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:kWXRequestLocalDataCompleteNotification object:nil];
}
#pragma mark -公开方法

- (void)requestHomeNewsWithRequestType:(WXNewsRequestType)requestType
{
    if (requestType == WXNewsRequestTypeFirst) {
        self.cloudVideoNewsAPIManager.isNextLoad = NO;
        [self.cloudVideoNewsAPIManager loadFirstData];
    } else {
        self.cloudVideoNewsAPIManager.isNextLoad = YES;
        if ([_homeNewsModelList count]>=self.cloudVideoNewsAPIManager.total) {
            [WXProgressHUD showText:@"没有更多数据"];
            self.isNoMoreDataList = YES;
            return;
        }
        [self.cloudVideoNewsAPIManager loadNextData];
    }
    self.isNoMoreDataList = NO;
}

#pragma mark -私有方法
- (NSArray *)handleADDataWithNewslIst:(NSArray *)newsList
{
    if (self.isHadADView == YES) {
        NSInteger listCount = self.adPlaceList.count+newsList.count;
        NSMutableArray *tempNewslist = [NSMutableArray arrayWithArray:newsList];
        for (NSString *adPlace in self.adPlaceList) {
            NSInteger adIndex = [adPlace integerValue];
            if (newsList.count>adIndex && adIndex>(newsList.count-10)) {
                WXBaseInteractiveModel *model = [WXBaseInteractiveModel new];
                model.modelUIType = WXBaseModelUITypeADImage;
                [tempNewslist insertObject:model atIndex:adIndex];
            }
        }
        return [tempNewslist copy];
    } else {
        return newsList;
    }
}



#pragma mark -懒加载

- (WXCloudVideoNewsAPIManager *)cloudVideoNewsAPIManager
{
    if (_cloudVideoNewsAPIManager == nil) {
        _cloudVideoNewsAPIManager = [WXCloudVideoNewsAPIManager new];
        _cloudVideoNewsAPIManager.paramSource = self;
        _cloudVideoNewsAPIManager.delegate = self;
    }
    return _cloudVideoNewsAPIManager;
}

- (NSString *)functionID
{
    NSString *custonFunctionID = nil;
    if (_cycleViewID != nil) {
        custonFunctionID = [NSString stringWithFormat:@"%@function",_cycleViewID];
        
    } else {
        custonFunctionID = @"functionID";
    }
    return custonFunctionID;
}

@end
